import { _decorator, Component, Node, Label, ScrollView, Button, RichText, UITransform } from 'cc';
const { ccclass, property } = _decorator;

enum MessageType {
    EVENT,
    NPC_ACTION,
    SYSTEN_MESSAGE,
    ALL,
}

@ccclass('MessageBoxManager')
export class MessageBoxManager extends Component {
    
    @property(ScrollView)
    scrollView: ScrollView = null;

    @property(UITransform)
    scrollViewContent: UITransform = null;

    @property(RichText)
    messageLabel1: RichText = null;

    @property([Button])
    filterButtons: Button[] = [];

    private messages: { type: MessageType, content: string }[] = [];
    private maxMessages: number = 25;
    private currentFilter: MessageType = MessageType.ALL;

    start() {
        this.filterButtons.forEach(button => {
            button.node.on(Button.EventType.CLICK, this.onFilterButtonClick, this);
        });

        // 确保 messageLabel 已经正确初始化
        if (!this.messageLabel1) {
            console.error('messageLabel is not initialized!');
        }
    }

    update(deltaTime: number) {
        
    }

    /**
     * 显示消息
     * @param type 消息类型
     * @param content 消息内容
     */
    public showMessage(type: MessageType, content: string) {
        this.messages.push({ type, content });
        if (this.messages.length > this.maxMessages) {
            this.messages.shift();
        }
        this.updateMessageLabel();
    }

    /**
     * 根据筛选条件更新消息标签
     */
    private updateMessageLabel() {
        if (!this.messageLabel1) {
            console.error('messageLabel1 is not initialized!');
            return;
        }

        let filteredMessages = this.messages;
        if (this.currentFilter !== MessageType.ALL) {
            filteredMessages = this.messages.filter(message => message.type === this.currentFilter);
        }

        // 合并颜色前缀和消息内容
        const coloredMessages = filteredMessages.map(message => {
            let prefix = '';
            let color = 'black'; // 默认颜色
            switch (message.type) {
                case MessageType.EVENT:
                    prefix = '【场景】';
                    color = '#88610F';
                    break;
                case MessageType.NPC_ACTION:
                    prefix = '【对话】';
                    color = '#333333';
                    break;
                case MessageType.SYSTEN_MESSAGE:
                    prefix = '【系统】';
                    color = '#531B1B';
                    break;
                default:
                    prefix = '【未知】';
                    color = 'black';
            }
            return `<color=${color}>${prefix}</color><color=${'#00000f'}>${message.content}</color>`;
        });

        // 设置合并后的消息到 messageLabel1
        this.messageLabel1.string = coloredMessages.join('\n');
        // 计算 messageLabel1 的高度
        const labelHeight = this.messageLabel1.node.getComponent(UITransform).height+10;
        // 设置 scrollview 的内容视图高度
        this.scrollViewContent.getComponent(UITransform).height = Math.max(labelHeight, 250);
        // this.scrollViewContent.getComponent(UITransform).height =labelHeight;

        this.scrollView.scrollToBottom();
    }

    /**
     * 切换筛选条件时的回调函数
     * @param button 点击的按钮
     */
    private onFilterButtonClick(button: Button) {
        const buttonName = button.node.name;
        if (buttonName === '场景') {
            this.currentFilter = MessageType.EVENT;
        } else if (buttonName === '对话') {
            this.currentFilter = MessageType.NPC_ACTION;
        } else if (buttonName === '系统') {
            this.currentFilter = MessageType.SYSTEN_MESSAGE;
        } else {
            this.currentFilter = MessageType.ALL;
        }
        this.updateMessageLabel();
    }
}


